package org.az.netty_study.p08_netty组件之handler和pipeline;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class _2_handler的细节 {
    public static void main(String[] args) {
        new ServerBootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        /**
                         * 下面添加了三个入站处理器
                         * head -> handler1 -> handler2 -> handler3 - tail
                         */
                        ch.pipeline().addLast("h1", new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.info("handler1");
                                super.channelRead(ctx, msg);
                            }
                        }).addLast("h2", new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.info("handler2");
                                super.channelRead(ctx, msg);
                            }
                        }).addLast("h3", new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.info("handler3");
                                //这里往channel里写入数据，会触发所有出站处理器，因为这个api是从tail开始往前找所有出站handler
                                ch.writeAndFlush(ctx.alloc().buffer().writeBytes("hello".getBytes()));
                                //也是往channel里写入数据，但不会触发所有出站处理器，因为这个api是在当前handler的基础上，往前找所有出站handler
                                //ctx.writeAndFlush(ctx.alloc().buffer().writeBytes("hello".getBytes()));
                                super.channelRead(ctx, msg);
                            }
                        });
                        /**
                         * 添加出站处理器，此时流水线上的工序
                         * head -> handler1(in) -> handler2(in) -> handler3(in) -> handler4(out) -> handler5(out) -> tail
                         * 根据观察到的现象
                         *  1.读取数据时，从head开始，依次回调后面的所有入站handler
                         *  2.写入数据时，从tail开始(也可能从当前handler开始)，依次回调前面的所有出站handler
                         */
                        ch.pipeline().addLast("h4", new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                log.info("handler4");
                                super.write(ctx, msg, promise);
                            }
                        }).addLast("h5", new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                log.info("handler5");
                                super.write(ctx, msg, promise);
                            }
                        });
                    }
                })
                .bind(9001);
    }
}
